package cn.usts.deu.controller;


import cn.usts.deu.pojo.User1;

import cn.usts.deu.utils.JsonUtils;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;


import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

// 配合 @ResponseBody 使用
//@Controller
@RestController  // 返回字符串，不经过视图解析器
public class UserController {
//    @RequestMapping(value = "/json1",produces = "application/json;charset=utf-8")  解决乱码问题
    // 多数通过配置xml文件实现乱码过滤，这种方式不推荐，写起来太冗余
    @RequestMapping(value = "/json1")
   // @ResponseBody // 让返回字符串对象，不走视图解析器
    public String json1() throws JsonProcessingException {
        ObjectMapper mapper = new ObjectMapper();
        User1 user1 = new User1("tom",12,"男");
        // 将对象转换为字符串对象，如果出现500错误多数是 打包没打jackson的包
        String st = mapper.writeValueAsString(user1);
        // 打印转换过的json数据
        System.out.println(st);
        return st;
    }

//    返回集合数据
    @RequestMapping("/json2")
    public String json2() throws JsonProcessingException {
        User1 user2 = new User1("tom2",13,"男");
        User1 user3 = new User1("tom3",11,"女");
        User1 user4 = new User1("tom4",14,"女");
        User1 user5 = new User1("tom5",12,"男");
        User1 user6 = new User1("tom6",11,"男");
        List<User1> list= new ArrayList<User1>();
        list.add(user2);
        list.add(user2);
        list.add(user3);
        list.add(user4);
        list.add(user5);
        list.add(user6);
        // 将集合转json字符串
        ObjectMapper mapper = new ObjectMapper();
        String str = mapper.writeValueAsString(list);
        return str;
    }

    // 返回日期对象
    @RequestMapping("/json3")
    public String json3() throws JsonProcessingException {
        ObjectMapper mapper = new ObjectMapper();
        // 创建一个时间对象
        Date data = new Date();
        String strData = mapper.writeValueAsString(data);
        return strData;
    }

    // 返回格式化日期对象
    @RequestMapping("/json4")
    public String json4() throws JsonProcessingException {
        ObjectMapper mapper = new ObjectMapper();
        // 不使用时间戳的方式
        mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS,false);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        System.out.println(sdf);
        // 设置 日期格式
        mapper.setDateFormat(sdf);
        Date date = new Date();
        // 设置字符串对象
        String str = mapper.writeValueAsString(date);
        return str;
    }

    @RequestMapping("/json5")
    public String json5(){
        Date date = new Date();
        return JsonUtils.getJson(date);
    }

    @RequestMapping("/json6")
    public String json6() throws JsonProcessingException {
        User1 user2 = new User1("tom2",13,"男");
        User1 user3 = new User1("tom3",11,"女");
        User1 user4 = new User1("tom4",14,"女");
        User1 user5 = new User1("tom5",12,"男");
        User1 user6 = new User1("tom6",11,"男");
        List<User1> list= new ArrayList<User1>();
        list.add(user2);
        list.add(user2);
        list.add(user3);
        list.add(user4);
        list.add(user5);
        list.add(user6);
        // 将集合转json字符串
        return JsonUtils.getJson(list);
    }


}